package com.oa.admin.controller;

import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.HouseAddDTO;
import com.oa.admin.dto.HouseConditionDTO;
import com.oa.admin.dto.HouseUpdateDTO;
import com.oa.admin.entity.House;
import com.oa.admin.service.IHouseService;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/house")
@Api(description = "房屋表模块")
public class HouseController extends BaseController {

    private final IHouseService houseService;

    @Autowired
    public HouseController(IHouseService houseService) {
        this.houseService = houseService;
    }

    /**
     * 查询列表
     *
     * @return Resp
     */
    @PostMapping("/findList")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询")
    public Resp findList(@ApiParam("房屋查询条件对象") HouseConditionDTO houseConditionDTO) throws DataIsEmptyException {
        return new Resp().success(houseService.findHouseListByCondition(houseConditionDTO));
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @PostMapping("/findById")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp findById(@ApiParam(value = "id", required = true) @RequestParam String id) {
        return houseService.findHouseDetailsById(id);
    }


    /**
     * 新增
     *
     * @param houseAddDTO HouseAddDTO
     * @return Resp
     */
    @PostMapping("/add")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "房屋表对象") @RequestBody(required = false) HouseAddDTO houseAddDTO) throws BusinessException {
        return houseService.addHouse(houseAddDTO);
    }

    /**
     * 根据Id修改
     *
     * @param houseUpdateDTO HouseUpdateDTO
     * @return Resp
     */
    @PutMapping("/update")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "房屋表对象") @RequestBody(required = false) HouseUpdateDTO houseUpdateDTO) throws BusinessException {
        return houseService.updateHouseDetailsById(houseUpdateDTO);
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        House house = houseService.selectById(id);
        if (house != null && house.getIsDeleted() == 2) {
            if (houseService.deleteHouse(id)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
